package com.akk.platform.manage;


import com.akk.module.avs.entity.AvsPartner;
import com.akk.module.avs.service.AvsPartnerService;
import com.akk.utils.PageUtils;
import com.akk.utils.Query;
import com.akk.utils.R;
import com.akk.utils.RestResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 后台系统合作商
 *
 * @company www.lovekaka.com
 * @auther hainlc
 * @create 2017/05/12 13:32
 */
@RestController
@RequestMapping("/platform/manage/partner")
public class PartnerAction {

    @Autowired
    private AvsPartnerService partnerService;

    /**
     *显示合作商列表
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:partner:list")
    public RestResponse list(Query query,HttpServletRequest request) {
        Map params = query.getSearch();
        if (!StringUtils.isEmpty(request.getParameter("partnerName")))
            params.put("partnerName",request.getParameter("partnerName"));
        PageHelper.startPage(query.getPage(),query.getLimit());
        List<AvsPartner> list = partnerService.queryByPartnerNameLike(params);
        return RestResponse.build(PageUtils.buildPage(list,(int) new PageInfo<>(list).getTotal(),query));
    }

    /**
     * excel批量上传
     */
    @PostMapping("/uploadExcel")
    @RequiresPermissions("sys:partner:upload")
    public RestResponse upload(MultipartFile file, String name) {
        return partnerService.uploadExcel(file);
    }

    /**
     *  批量保存
     */
    @PostMapping("/saveAll")
    @RequiresPermissions("sys:partner:saveAll")
    public RestResponse saveAll(@RequestBody String json) {
        partnerService.saveAll(json);
        return RestResponse.build("success");
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{partnerId}")
    @RequiresPermissions("sys:partner:info")
    public RestResponse info(@PathVariable("partnerId") Long partnerId){
        AvsPartner partner = partnerService.queryById(partnerId);
        return RestResponse.build(partner);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:partner:save")
    public RestResponse save(@RequestBody AvsPartner partner){
        partnerService.save(partner);
        return RestResponse.build("success");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:partner:update")
    public RestResponse update(@RequestBody AvsPartner partner){
        partnerService.update(partner);
        return RestResponse.build("success");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:partner:delete")
    public RestResponse delete(@RequestBody Long[] partnerIds){
        partnerService.deleteBatchByPartnerIds(partnerIds);
        return RestResponse.build("success");
    }

}
